/*
 * 描述 资金接口
 * Create by cy on 2018/10/25
 * */

const express = require('express');
const router = express.Router();
const jwt = require('jsonwebtoken');
const Profile = require('../../models/Profile');
const keys = require('../../config/key');//拿加密姓名
const gravatar = require('gravatar');
const passport = require('passport');

/*
 * $router GET api/users/test
 * @return 返回json
 * @assess  public
 * */
router.get('/test', (req, res) => {
    res.json({msg: 'PROFILE works'})
});
/*
 * $router POST api/profiles/add
 * @return 返回json
 * @assess  private
 * */
// new Profile({
//     type:'转账',
//     describe:'买学习资料',
//     income:5000,
//     expend:200,
//     cash:3000,
//     remark:'study'
// }).save()
router.post('/add',passport.authenticate('jwt', {session: false}), (req, res) => {
    const profileFields ={};//创建对象
    if(req.body.type) profileFields.type = req.body.type;//如果此属性存在赋给对象
    if(req.body.describe) profileFields.describe = req.body.describe;
    if(req.body.income) profileFields.income = req.body.income;
    if(req.body.expend) profileFields.expend = req.body.expend;
    if(req.body.cash) profileFields.cash = req.body.cash;
    if(req.body.remark) profileFields.remark = req.body.remark;

    new Profile(profileFields).save().then(profile=>{
        res.json(profile);//如果成功,把数据返回
    })

});
/*
 * $router get api/profiles/delete:id
 * @return 删除信息接口
 * @assess  public
 * */
router.get('/delete/:id',passport.authenticate('jwt', {session: false}), (req, res) => {
    console.log('delete',req.params.id)
    Profile.findOneAndRemove({_id:req.params.id}).then(profile=>{
        profile.save().then(profile=>{
            res.json(profile);
        })
    });
});


/*
 * $router POST api/profiles/edit
 * @return 编辑信息接口
 * @assess  public
 * */
router.post('/edit/:id',passport.authenticate('jwt', {session: false}), (req, res) => {
    const profileFields ={};//创建对象
    if(req.body.type) profileFields.type = req.body.type;//如果此属性存在赋给对象
    if(req.body.describe) profileFields.describe = req.body.describe;
    if(req.body.income) profileFields.income = req.body.income;
    if(req.body.expend) profileFields.expend = req.body.expend;
    if(req.body.cash) profileFields.cash = req.body.cash;
    if(req.body.remark) profileFields.remark = req.body.remark;

    Profile.findOneAndUpdate(
        {_id:req.params.id},
        {$set:profileFields},
        {new:true},
        ).then(profile=>{
        res.json(profile);
    })
});

/*
 * $router POST api/profiles/
 * @return 获取所有信息
 * @assess  public
 * */
router.get('/',passport.authenticate('jwt', {session: false}), (req, res) => {
    Profile.find().then(profile=>{
        if(!profile){
            return res.status(400).json('没有获取到信息');
        }else{
            res.json(profile);
        }
    })
});

/*
 * $router POST api/profiles/:ID
 * @return 根据ID获取单个信息
 * @assess  public
 * */
router.get('/:id',passport.authenticate('jwt', {session: false}), (req, res) => {
    Profile.findOne({_id:req.params.id}).then(profile=>{
        if(!profile){
            return res.status(400).json('没有获取到信息');
        }else{
            res.json(profile);
        }
    })
});

/*
 * $router GET api/users/current:请求当前用户的信息,当前用户需要携带有效的token去请求
 * @return 返回 current user---验证token可以用passport和passport-jwt
 * @assess private:只有拥有token(钥匙/令牌)才能请求到当前信息
 * */


router.get('/current',passport.authenticate('jwt', {session: false}), (req, res) => {
    res.json({
        //这里能拿到的users的信息,是passport.js返回的
        id: req.user.id,
        name: req.user.name,
        email: req.user.email,
        identity: req.user.identity
    });
});
module.exports = router;